package com.mg.operating.service.impl;

import com.mg.operating.entity.ConfirmVo;
import com.mg.operating.entity.VideoComments;
import com.mg.operating.entity.VideoManage;
import com.mg.operating.mapper.VideoCommentsMapper;
import com.mg.operating.mapper.VideoManageMapper;
import com.mg.operating.service.ViCommentService;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ViCommentServiceImpl implements ViCommentService {

    @Resource
    VideoCommentsMapper videoCommentsMapper;

    @Resource
    VideoManageMapper videoManageMapper;


    private static List list = new ArrayList();
    /**
     * 用于添加评论点赞数，调用第一次点赞数增加，并记录点赞的视频名。
     * 第二次调用如果视频名存在则该视频以点赞过，则为取消点赞
     * @param id 要操作的视频对象id
     * @return 操作成功返回1，失败返回0
     */
    public int like(int id) {
        VideoComments videoComments = videoCommentsMapper.selectById(id);
        int i = 0;
        if (!list.contains(videoComments.getName())) {
            list.add(videoComments.getName());
            videoComments.setFoLikes(videoComments.getFoLikes() + 1);
            i = videoCommentsMapper.updateById(videoComments);
        }else {
            list.remove(videoComments.getName());
            videoComments.setFoLikes(videoComments.getFoLikes() - 1);
            i = videoCommentsMapper.updateById(videoComments);
        }
        return i;
    }

    /**
     * 评论转发数，调用转发数并成功则增加1
     * 如果id为传入的id,confirm为0,则返回转发路径
     * 如果id为传入的id,confirm为1,表示确认转发,则跳转并返回address="1"
     * @param id 要操作的视频对象id
     * @param confirm 判断是否确认转发值1为确认
     * @return 操作成功返回1，失败返回0
     */
    public ConfirmVo forWardPlus(int id, int confirm) {
        ConfirmVo confirmVo = new ConfirmVo();
        if (confirm==1){
            new ModelAndView("redirect:https://qzone.qq.com/");
            VideoComments videoComments = videoCommentsMapper.selectById(id);
            videoComments.setFoForward(videoComments.getFoForward() + 1);
            int i = videoCommentsMapper.updateById(videoComments);
            confirmVo.setId(id);
            confirmVo.setAddress(i+"");
            return confirmVo;
        }
        confirmVo.setId(id);
        confirmVo.setAddress("https://qzone.qq.com/");
        return confirmVo;
    }

    /**
     * 添加评论,添加成功后再通过统计更新评论数
     * 先插后统计再查最后修改
     * insert: 插入数据
     * count: 统计数据
     * upData: 动态修改数据
     * @param videoComments 操作评论的对象
     * @return 操作成功返回1，失败返回0
     */
    public int commentPlus(VideoComments videoComments){
        int i = videoCommentsMapper.insert(videoComments);
        if (i==1){
            int count = videoCommentsMapper.count(videoComments.getUid());
            VideoManage videoManage = new VideoManage();
            videoManage.setCom_number(count);
            videoManage.setId(videoComments.getUid());
            videoManageMapper.upData(videoManage);
        }
        return i;
    }

    /**
     * 删除评论,删除成功后再通过统计更新评论数
     * 先插后统计再查最后修改
     * selectById: 通过id查询数据
     * getUid: 主表id为附表uid,因此获取附表uid
     * deleteById: 通过id删除
     * count: 统计数据
     * upData: 动态修改数据
     * @param id 操作评论的对象id
     * @return 操作成功返回1，失败返回0
     */
    public int commentLess(int id){
        Integer uid = videoCommentsMapper.selectById(id).getUid();
        int i = videoCommentsMapper.deleteById(id);
        if (i==1){
            int count = videoCommentsMapper.count(uid);
            VideoManage videoManage = new VideoManage();
            videoManage.setCom_number(count);
            videoManage.setId(uid);
            videoManageMapper.upData(videoManage);
        }
        return i;
    }

}
